<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page contentType="text/html;charset=UTF-8" %>
<%@ include file="/commons/taglibs.jsp" %>
<html>
	<head>
		<script src="${ctx}/js/common/jquery-1.6.2.min.js" type="text/javascript"></script>
		<%--
		<script src="${ctx}/includes/jquery.js" type="text/javascript"></script>
		 --%>
		<style type="text/css">
			.TABLE {
				TABLE-LAYOUT: fixed
			}
			.TD {
				LEFT: 10px;
				WIDTH: 100%;
				WORD-WRAP: break-word
			}
			.maintable {
				width: 100%;
				border-collapse: collapse
			}
			.maintable .tr1 td{BACKGROUND-COLOR: #ebf7f8;}
			.maintable tbody td {
				border: 1px solid #9fccec;
				padding: 3px 4px;
				vertical-align: middle;
				HEIGHT: 25px;
				font-size: 12px;
				font-family: Arial,Verdana,Helvetica,sans-serif,"宋体";
				white-space: normal
			}
		</style>
	</head>
	<body>
		<form action="${ctx}/pub/userAllByDeptSelect.jsp" id="user_select_form">
			<table>
				<tr>
					<td>
						部门<select id="deptSelector" onchange="selectDept(this)" name="keyid"></select>
						<input name="key" class="text-box" id="key"/>
						<input type="button" value="查询" onclick="searchUser()"/>
					</td>
				</tr>
			</table>
			<table class="maintable">
				<tr class="tr1">
					<td width="50%"><input class="checkall" type="checkbox"/>姓名</td>
					<td width="50%">部门</td>
				</tr>
			</table>
			<div style="height: 330px;">
			<table class="maintable" id="gridList">
			</table>
			</div>
			<%-- 
			<table width="100%">
				<tr>
					<td align="center"><input type="button" onclick="selectOnlyUser();" value="确定" style="width: 60px;height: 30px"/></td>
				</tr>
			</table>
			--%>
		</form>
	</body>
</html>
<script type="text/javascript">
	loadDept();
	
	function selectMoreUser(){
		var selectItem = parent.document.getElementById("users");
           var isExisted = false;
           $('.checkitem:checked').each(function(){
               var ygbh = this.value;
               var ygxm = this.uname;
               for (var x = 0; x < selectItem.length; x++) {
					if (ygbh == selectItem[x].value) {
						isExisted=true;
						break;
					}	
				}
				if (!isExisted) {
					var optionEl = document.createElement("OPTION");
					optionEl.text = ygxm;
					optionEl.value = ygbh;
					optionEl.selected = true;
					selectItem.options.add(optionEl);
				}
				isExisted = false;
           });
	}
	function selectOnlyUser(){
           if($('.checkitem:checked').length == 0 || $('.checkitem:checked').length > 1){    
				alert("请选择一个用户！");
	            return false;
	        }else{
	        	var userid = $('.checkitem:checked')[0].value;
	        	var username = $('.checkitem:checked').attr('uname');
	        	parent.document.getElementById('zrrbh').value = userid;
	        	parent.document.getElementById('zrr').value = username;
	        	return true;
	        }
	}
	
	function selectMoreUserByTask(){
           var selectItem = parent.taskprocess.document.getElementById("users");
           var isExisted = false;
           $('.checkitem:checked').each(function(){
               var ygbh = this.value;
               var ygxm = this.uname;
               for (var x = 0; x < selectItem.length; x++) {
					if (ygbh == selectItem[x].value) {
						isExisted=true;
						break;
					}	
				}
				if (!isExisted) {
					var optionEl = document.createElement("OPTION");
					optionEl.text = ygxm;
					optionEl.value = ygbh;
					optionEl.selected = true;
					selectItem.options.add(optionEl);
				}
				isExisted = false;
           });
	}
	
	function selectOnlyUserByTask(){
           if($('.checkitem:checked').length == 0 || $('.checkitem:checked').length > 1){    
				alert("请选择一个用户！");
	            return false;
	        }else{
	        	var userid = $('.checkitem:checked')[0].value;
	        	var username = $('.checkitem:checked').attr('uname');
	        	parent.taskprocess.document.getElementById('zrrbh').value = userid;
	        	parent.taskprocess.document.getElementById('zrr').value = username;
	        	return true;
	        }
	}
	
	function searchUser(){
		var deptid = $('#deptSelector').val();
		var realname = encodeURIComponent($('#key').val());
		jQuery.ajax({
		   	type:'post',	
		   	url:'${ctx}/user/searchUser.do?keyid='+deptid+'&key='+realname+'&timestamp='+new Date().getTime(),
		   	dataType:'xml',
		   	success:updateItemListCallBack,
		    error:updateItemListCallBack
		});
	}
	function loadDept(){
	jQuery.ajax({
		   	type:'post',	
		   	url:'${ctx}/dept/selectMyDept.do?keyid=${sessionScope.loginInfo.topdeptid}&timestamp='+new Date().getTime(),
		   	dataType:'xml',
		   	success:resultHandler,
		    error:resultHandler,
		    processData:false
		});
	}
	
	function selectDept(dept){
		jQuery.ajax({
		   	type:'post',	
		   	url:'${ctx}/user/searchUser.do?keyid='+dept.value+'&timestamp='+new Date().getTime(),
		   	dataType:'xml',
		   	success:updateItemListCallBack,
		    error:updateItemListCallBack
		});
		
	}
	
	function updateItemListCallBack(response){
		var data = eval("("+response.responseText+")");
		var totalRecords = data.totalRecords;
		data = data.root;
		if(data)
		{
		    var table = $("#gridList"),html="";
		    table.html('');
			for(var i=0,iilen=data.length;i<iilen;i++)
			{
				html = ["<tr height='30' class='gray'><td width='50%'><input name='OrgSelector_ChkItem' class='checkitem' type='checkbox' value='",data[i].id,"' uname='",data[i].name,"'/>",data[i].name,"</td><td>",data[i].orgInfo,"</td></tr>"].join("");
        		table.append(html);
			}
		}
	}
	
	function resultHandler(response){
		var data = eval("("+response.responseText+")");
		data = data.root;
		if(data)
		{
		    var deptSelector = document.getElementById("deptSelector").options;
			for(var i=0,iilen=data.length;i<iilen;i++)
			{
				var o = document.createElement("OPTION");
				o.text = getTreeEntry(data[i].name,data[i].orgInfo);
				var oBuffer=new Array;
				oBuffer.push(data[i].id);
				oBuffer.push("");
				oBuffer.push(data[i].parentDeptID);					
				o.value = oBuffer.join("");
				deptSelector.options.add(o);
			}
		}
	}
	function getTreeEntry(deptName,deptPath)
	{
		var treeEntry = new Array;
		var depth = deptPath.split("-").length;
		for(var i=1;i<depth;i++)
		{
			treeEntry.push("│");
		}
		treeEntry.push("├");
		treeEntry.push(deptName);
		return treeEntry.join("");
	}
	function resultHandlerError(response){
		alert('系统繁忙，请重试！');
	}
	
	$('.checkall').click(function(){
        $('.checkitem').attr('checked', this.checked)
    });
</script>